package com.madinatek.designproject.client;
import javax.media.bean.playerbean.*;
import javax.media.*;
import javax.media.format.*;
import javax.media.protocol.*;
import javax.media.control.*;
import java.util.*;
import java.awt.*;
import com.sun.media.ui.VideoFormatChooser;
import javax.swing.*;
import java.awt.event.*;

public class Main3 implements ControllerListener {
	MediaPlayer mp;
	JFrame frame;
	DataSource ds;
	VideoFormatChooser chooserVideo;	
	public Main3() {
		try {
		Vector devices = CaptureDeviceManager.getDeviceList(null);
		Format arrFormats[];
		
		for(int i =0; i < devices.size(); i++)
		{
			CaptureDeviceInfo dinfo = (CaptureDeviceInfo) devices.elementAt(i);
			//System.out.println(dinfo);
		}
		
		CaptureDeviceInfo firewireCam = (CaptureDeviceInfo) devices.elementAt(2);
		
		System.err.println("FireWire cam: " + firewireCam);
		
		arrFormats = firewireCam.getFormats();
		
		frame = new JFrame("test");
		//chooserVideo = new VideoFormatChooser(arrFormats, (VideoFormat) arrFormats[0], false, null, true);
		
		//frame.add(chooserVideo, BorderLayout.NORTH);
		
		VideoFormat sf = (VideoFormat) arrFormats[0];
		
		
		
		Format myformat = new VideoFormat(sf.getEncoding(), sf.getSize(), sf.getMaxDataLength(), sf.getDataType(), 30.0f);
		
		//System.err.println("Choosen format: " + myformat);
		
		
		
		ds = Manager.createDataSource(firewireCam.getLocator());
		
		FormatControl [] frmtCtrl = ((CaptureDevice) ds).getFormatControls();
		frmtCtrl[0].setFormat(myformat);
		//System.err.println(frmtCtrl[0].getSupportedFormats()[0]);
		/*
		for(int i=0; i < frmtCtrl.length; i++)
		{
			Format [] formats = frmtCtrl[i].getSupportedFormats();
			for(int j=0; j < formats.length; j++) {
				if(formats[j].matches(myformat))
					frmtCtrl[i].setFormat(myformat);
			}
		}
		*/
		mp = new MediaPlayer();
		mp.setDataSource(ds);
		mp.realize();
		mp.addControllerListener(this);
		
		//JButton update = new JButton("update");
		//update.addActionListener(this);
		
		//frame.add(update, BorderLayout.CENTER);
		frame.setSize(400,600);
		
		} catch (Exception ex) { ex.printStackTrace(); }
	}
	
	public static void main(String [] agrs) throws Exception {
		new Main3();	
	}
	
	public synchronized void controllerUpdate (ControllerEvent event) {
		if(event instanceof RealizeCompleteEvent) {
			Component comp = mp.getVisualComponent();
			
			mp.start();
			
			frame.add(comp, BorderLayout.SOUTH);
		
			frame.show();	
		}
	}
}
